package com.stenio.fancat.util;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.naming.NoNameCoder;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class XmlUtil {

    private static final Logger logger = LoggerFactory.getLogger(XmlUtil.class);

    public static String fromBean(Object o) {
        XStream xStream = new XStream(new XppDriver(new NoNameCoder()));
        xStream.autodetectAnnotations(true);
        xStream.alias("xml", o.getClass());
        return xStream.toXML(o);
    }

    public static <T> T toBean(String xml, Class<T> clazz) throws IllegalAccessException, InstantiationException {
        XStream xStream = new XStream(new XppDriver(new NoNameCoder()));
        xStream.alias("xml", clazz);
        return (T) xStream.fromXML(xml, clazz.newInstance());
    }
}
